package com.jwell.panrank.ui.adapter

import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.jwell.panrank.R
import com.jwell.panrank.model.PreRegisterModel
import kotlinx.android.synthetic.main.item_pre_register_driver_layout.view.*

/**
 * Created by 76577 on 2018/4/11.
 * 预登记增加司机
 */
class PreRegisterDriverAdapter constructor(list: ArrayList<PreRegisterModel.DriverInfo>) : RecyclerView.Adapter<PreRegisterDriverAdapter.ViewHolder>() {
    private var list = list
    private var chooseListener: ChooseListener? = null
    override fun getItemCount(): Int {
        return list.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_pre_register_driver_layout, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        with(holder.itemView) {
            text_chooseCarNum.text = list[position].carNum
            text_driverName.text = list[position].driverName
            text_driverPhone.text = list[position].driverPhone
            text_chooseCarNum.setOnClickListener {
                chooseListener?.chooseCarNum(position)
            }
            text_driverName.setOnClickListener {
                chooseListener?.chooseDriver(position)
            }
            text_driverPhone.setOnClickListener {
                chooseListener?.chooseDriver(position)
            }
            text_delete.setOnClickListener {
                if (list.size > 1) {
                    list.removeAt(position)
                    notifyDataSetChanged()
                } else {
                    Toast.makeText(holder.itemView.context, "至少调度一位司机", Toast.LENGTH_SHORT).show()
                }
            }
            //解决editText数据错乱
            text_remark.setText("")
            if (list[position].remark != null) {
                text_remark.setText(list[position].remark)
            } else {
                text_remark.setText("")
            }
            val textWatcherListener = object : TextWatcher {
                override fun afterTextChanged(s: Editable?) {
                    s?.isEmpty()?.let {
                        if (it) {
                            list[position].remark = null
                            return
                        }
                    }
                    try {
                        s?.toString()?.let {
                            list[position].remark = it
                        }
                    } catch (e: Exception) {
                        list[position].remark = null
                    }
                }

                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                    s?.let {
                        if (it.length > 15) {
                            val ss = it.toString().subSequence(0, 15)
                            text_remark.setText(ss)
                        }
                    }
                }
            }
            text_remark.setOnFocusChangeListener { v, hasFocus ->
                if (hasFocus) {
                    text_remark.addTextChangedListener(textWatcherListener)
                } else {
                    text_remark.removeTextChangedListener(textWatcherListener)
                }
            }
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    interface ChooseListener {
        //选择车牌
        fun chooseCarNum(position: Int)

        //选择司机信息
        fun chooseDriver(position: Int)

    }

    fun setChooseListener(listener: ChooseListener) {
        this.chooseListener = listener
    }
}